<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item class="mr" label="监测信息" prop="monitorInfo">
        <el-input
          v-model="queryParams.monitorInfo"
          placeholder="请输入"
          clearable
          style="width: 220px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="mr" label="监测设备" prop="monitorDevice">
        <el-select
          v-model="queryParams.monitorDevice"
          placeholder="请选择"
          clearable
          style="width: 220px"
        >
          <el-option
            v-for="{ label, value } in mocks"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="mr" label="报警级别" prop="alarmLevel">
        <el-select
          v-model="queryParams.alarmLevel"
          placeholder="请选择"
          clearable
          style="width: 220px"
        >
          <el-option
            v-for="{ label, value } in sys_alarm_level"
            :key="value"
            :label="label"
            :value="value"
          />
        </el-select>
      </el-form-item>
      <el-form-item style="margin-left: 30px">
        <el-button
          type="primary"
          icon="Search"
          @click="handleQuery"
          v-hasPermi="['observe:preset:query']"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['observe:preset:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['observe:preset:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['observe:preset:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['observe:preset:export']"
          >导出
        </el-button>
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="presetList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" width="80" align="center" type="index" />
      <el-table-column label="监测信息" align="center" prop="monitorInfo" />
      <el-table-column label="数值" align="center" prop="numericalValue" />
      <el-table-column label="上下阈值" align="center" prop="threshold">
        <template #default="{ row }">
          {{ row.threshold ? '±' + row.threshold : 0 }}
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="监测设备" align="center" prop="monitorDevice">
        <template #default="{ row }">
          <dict-tag :options="mocks" :value="row.monitorDevice" />
        </template>
      </el-table-column>
      <el-table-column label="报警级别" align="center" prop="alarmLevel">
        <template #default="{ row }">
          <dict-tag :options="sys_alarm_level" :value="row.alarmLevel" />
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="text"
            icon="View"
            @click="handleView(scope.row)"
            v-hasPermi="['observe:preset:detail']"
            >详细
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通风口对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form :model="form" :rules="rules" ref="presetRef" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="监测信息" prop="monitorInfo">
              <el-input
                v-model="form.monitorInfo"
                placeholder="请输入"
                maxlength="30"
                style="width: 180px"
              />
            </el-form-item>
            <el-form-item label="数值" prop="numericalValue">
              <el-input-number
                v-model="form.numericalValue"
                placeholder="请输入"
                :min="0"
                style="width: 180px"
              />
            </el-form-item>
            <el-form-item label="上下阀值(±)" prop="threshold">
              <el-input-number
                v-model="form.threshold"
                placeholder="请输入"
                :min="0"
                style="width: 180px"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input
                v-model="form.unit"
                placeholder="请输入"
                maxlength="30"
                style="width: 180px"
              />
            </el-form-item>
            <el-form-item label="监测设备" prop="monitorDevice">
              <el-select
                v-model="form.monitorDevice"
                placeholder="请选择"
                style="width: 180px"
              >
                <el-option
                  v-for="{ label, value } in mocks"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="报警级别" prop="alarmLevel">
              <el-select
                v-model="form.alarmLevel"
                placeholder="请选择"
                style="width: 180px"
              >
                <el-option
                  v-for="{ label, value } in sys_alarm_level"
                  :key="value"
                  :label="label"
                  :value="value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="observePreset">
import {
  listPreset,
  addPreset,
  updatePreset,
  delPreset,
  getPreset
} from '@/api/environment/observe/preset'

const { proxy } = getCurrentInstance()

/* 获取单位字典 */
const { sys_alarm_level } = proxy.useDict('sys_alarm_level') //报警级别
const mocks = [
  {
    label: '设备1',
    value: '1'
  },
  {
    label: '设备2',
    value: '2'
  },
  {
    label: '设备3',
    value: '3'
  }
]

const presetList = ref([])
const open = ref(false)
const loading = ref(true)
const sns = ref([])
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const openView = ref(false)
const title = ref('')
const total = ref(0)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  rules: {
    monitorInfo: [
      { required: true, message: '监测信息不能为空', trigger: 'blur' }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询通风口列表 */
function getList() {
  loading.value = true
  listPreset(queryParams.value).then((response) => {
    presetList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

/* 重置新增和修改表单 */
function reset() {
  form.value = {}
  proxy.resetForm('presetRef')
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加预设值信息'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const presetId = row.id || ids.value
  getPreset(presetId).then((response) => {
    form.value = response.data
    open.value = true
    title.value = '修改预设值信息'
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const presetIds = row.id || ids.value
  proxy.$modal
    .confirm('是否确认删除序号为："' + sns.value + '"的数据项?')
    .then(function () {
      return delPreset(presetIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'enc/monitor/export',
    {
      ...queryParams.value
    },
    `monitor_${new Date().getTime()}.xlsx`
  )
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['presetRef'].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePreset(form.value).then((response) => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addPreset(form.value).then((response) => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 把每一行的索引放进row */
function tableRowClassName({ row, rowIndex }) {
  row.index = rowIndex + 1
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  sns.value = selection
    .map((item) => +item.index)
    .sort(function (a, b) {
      return a - b
    })
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 详细按钮操作 */
function handleView(row) {
  openView.value = true
  form.value = row
}

// 初始化列表
getList()
</script>
<style lang="scss" scoped>
.mr {
  margin-right: 5px;
}
</style>
